package com.yk.aop;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class LogAop {
    //定义切面类
    @Pointcut("execution(public * com.yk.service.*.*(..))")
    public void log(){};


    //方法执行前通知
    @Before("log()")
    public void before(JoinPoint joinPoint){
        //获取方法的入参
        Object[] args = joinPoint.getArgs();
        //获取哪个方法被执行
        Signature signature = joinPoint.getSignature();
        log.info("当前正在执行的方法为:{},参数为:{}",signature,args);
    }
    //返回后通知
    @AfterReturning(pointcut = "log()",returning = "result")
    public void afterReturning(Object result){
        log.info("当前方法返回的结果为:{}", JSONObject.toJSONString(result));
    }
}
